from dataclasses import dataclass
from enum import Enum
from typing import TypeVar, Generic, Self

from core.utils.model import EnumValueText


class ResultCodeEnum(Enum):
    """
    定义返回码枚举,枚举的值，可以在自定义对象，也可以是字典，列表，元组等，如这里使用 自定义对象 EnumValueText
    """

    SUCCESS = EnumValueText(1, "Success")
    """
    成功
    """

    FAIL = EnumValueText(2, "Fail")
    """
    失败
    """


# 定义一个类型变量,bound 属性，可以指定它是哪种类型，如 T = TypeVar('T',bound=list)
T = TypeVar('T')


@dataclass
class JsonResult(Generic[T], object):
    """
    泛型类
    """

    code: int

    message: str

    data: T | None = None

    @classmethod
    def success(cls, message: str = ResultCodeEnum.SUCCESS.value.label, data: T = None) -> Self:
        """
            create a success instance
        :param message:
        :param data:
        :return:
        """
        return JsonResult[T](code=ResultCodeEnum.SUCCESS.value.value, message=message, data=data)

    @classmethod
    def fail(cls, message: str = ResultCodeEnum.FAIL.value.label, data: T = None) -> Self:
        """
            create a fail instance.
        :param message:
        :param data:
        :return:
        """
        return JsonResult[T](code=ResultCodeEnum.FAIL.value.value, message=message, data=data)
